﻿using UnityEngine;
using System.Collections;

public class PlayerJumpScript : MonoBehaviour {

	public float jumpForce = 10f;
	public Vector2 jumpDirection;
	public bool isGrounded = false;
	public bool isJumping = false;

	private Vector3 mousePosition;


	void Start () 
	{
		jumpDirection = new Vector2 ();
		mousePosition = new Vector3 ();
	}

	void Update()
	{
		if (Input.GetMouseButton(0)) {
			//si esta en el piso agrega impulso
			if (isGrounded && !isJumping){
				//jumpDirection.Normalize ();
				rigidbody2D.AddForce (jumpDirection * jumpForce, ForceMode2D.Impulse);
				isJumping = true;
			}

		}
	}

	void FixedUpdate () 
	{
		mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
		//jumpDirection.x = mousePosition.x - gameObject.transform.position.x;
		//jumpDirection.y = mousePosition.y - gameObject.transform.position.y;


		if (mousePosition.x <= - 1) {
			jumpDirection.x = -0.5f;
			jumpDirection.y = 0.8f;
		} else if (mousePosition.x >= 1) {
			jumpDirection.x = 0.5f;
			jumpDirection.y = 0.8f;
		} else {
			jumpDirection.x = 0f;
			jumpDirection.y = 0.75f;
		}
	}


	void  OnCollisionEnter2D(Collision2D collision)
	{
		//si toca el piso, setea isGrounded a true
		if(collision.gameObject.tag == "Ground")
		{
			isGrounded = true;
			isJumping = false;
		}
	}

	void  OnCollisionExit2D(Collision2D collision)
	{
		//si deja de tocar el piso, setea isGrounded a false
		if(collision.gameObject.tag == "Ground")
		{
			isGrounded = false;
		}
	}

}
